import { createSlice } from "@reduxjs/toolkit";
import { UserInfoType } from "../../types/user";
import store2 from "store2";
interface UserStateType {
  userInfo: null | UserInfoType;
}
let initialState: UserStateType = {
  userInfo: null,
};
//尝试提取本地存储数据
let uinfo = store2.get("react-mobile-2301");
if (uinfo) {
  initialState.userInfo = uinfo;
}
const userSlice = createSlice({
  name: "user",
  initialState,
  reducers: {
    initUserInfo(state, action) {
      state.userInfo = action.payload;
      store2.set("react-mobile-2301", action.payload);
    },
    //退出登录
    logout(state) {
      state.userInfo = null;
      store2.remove("react-mobile-2301");
    },
  },
});

export const { initUserInfo, logout } = userSlice.actions;
export default userSlice.reducer;
